//
//  HTGoodsTypeBoardView.m
//  TruckUser
//
//  Created by Ting Han on 2017/11/1.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTGoodsTypeBoardView.h"
#import "HTLineCollectionCell.h"
#import "HTGoodsTypeFooterReusableView.h"
@interface HTGoodsTypeBoardView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@property (nonatomic,strong) NSArray *dataSource;

@property (nonatomic,assign) CGFloat off;

@property (nonatomic,weak) HTGoodsTypeFooterReusableView *footerView;

@end

@implementation HTGoodsTypeBoardView

- (void)awakeFromNib{
    [super awakeFromNib];
    [self.flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    self.flowLayout.itemSize = CGSizeMake((kHTScreenWidth - 60)/5, kWidth(35));
    self.flowLayout.minimumLineSpacing = 10;
    self.flowLayout.minimumInteritemSpacing = 12;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HTLineCollectionCell" bundle:nil] forCellWithReuseIdentifier:LineCollectionCell];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HTGoodsTypeFooterReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:GoodsTypeFooterReusableView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Trucks" ofType:@"plist"];
    NSDictionary *dic =[NSDictionary dictionaryWithContentsOfFile:path];
    self.dataSource =  dic[@"GoodsType"];
    [self notifi];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HTLineCollectionCell *normal = [collectionView dequeueReusableCellWithReuseIdentifier:LineCollectionCell forIndexPath:indexPath];
    normal.state = 3;
    normal.titleLabel.text = self.dataSource[indexPath.row];
    return normal;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *reusableView = nil;
    if (kind == UICollectionElementKindSectionFooter)
    {
        if (indexPath.section == 0) {
            HTGoodsTypeFooterReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:GoodsTypeFooterReusableView forIndexPath:indexPath];
            self.mainTextField = footerview.mainTextField;
            self.mainButton = footerview.conformButton;
            self.mainTextField.delegate = self;
            self.footerView = footerview;
            [footerview.conformButton addTarget:self action:@selector(didChoseButton) forControlEvents:UIControlEventTouchUpInside];
            reusableView = footerview;
        }
    }
    return reusableView;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    
   return CGSizeMake(kHTScreenWidth, 50);
    
}
- (void)notifi{
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    
    [defaultCenter addObserver:self selector:@selector(keyboardWillShowOrHide:) name:UIKeyboardWillShowNotification object:nil];
    [defaultCenter addObserver:self selector:@selector(keyboardWillShowOrHide:) name:UIKeyboardWillHideNotification object:nil];
    
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillShowOrHide:(NSNotification *)notification
{
    //    //获取通知名
    NSString *notificationName = notification.name;
    if ([notificationName isEqualToString:UIKeyboardWillShowNotification]) {
        self.mainButton.hidden = NO;
        NSDictionary *keyboardInfo = notification.userInfo;
        CGRect keyboardF = [keyboardInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGRect fra = [self convertRect:keyboardF fromView:nil];
        CGFloat keyboardHeight = fra.size.height;
        
        CGRect selfFrameFromUIWindow = [self.mainTextField convertRect:self.mainTextField.bounds toView:nil];
        // textField底部距离屏幕底部的距离
        CGFloat bottomHeight =kHTScreenHeight - selfFrameFromUIWindow.origin.y - selfFrameFromUIWindow.size.height;
        CGFloat off =keyboardHeight - bottomHeight + 10;
        if(off > 0)
            
        {  self.off = off;
            CGPoint oldPoint = self.collectionView.contentOffset;
            CGPoint scrollPoint = CGPointMake(0.0 + oldPoint.x, off + oldPoint.y);
            [self.collectionView setContentOffset:scrollPoint animated:YES];
        }
    }
    if ([notificationName isEqualToString:UIKeyboardWillHideNotification]) {
        if (self.mainTextField.text == nil || self.mainTextField.text.length <= 0) {
            self.mainButton.hidden = YES;
        }
        if (self.off > 0) {
            CGPoint scrollPoint = CGPointMake(0.0, 0);
            [self.collectionView setContentOffset:scrollPoint animated:YES];
            self.off = 0;
        }
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return NO;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == self.mainTextField) {
        if (textField.text.length >= 4 && string.length > 0) {
            return NO;
        }else{
            return YES;
        }
    }
    return YES;
}

#pragma mark --- 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *item = self.dataSource[indexPath.row];
    if (self.delegate && [self.delegate respondsToSelector:@selector(didChoseItem:)]) {
        [self.delegate didChoseItem:item];
    }
}
- (void)didChoseButton{
    if (self.mainTextField.text == nil || self.mainTextField.text.length <= 0) {
        [HTShowUtil showMessage:@"请输入类型"];
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(didChoseItem:)]) {
        [self.delegate didChoseItem:self.mainTextField.text];
    }
}

@end
